package com.lanou.service.impl;

import com.lanou.entity.CatTree;
import com.lanou.entity.TbItemCat;
import com.lanou.entity.TbItemCatExample;
import com.lanou.mapper.TbItemCatMapper;
import com.lanou.service.TbItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QuChao on 2018/11/9
 */
@Service
public class TbItemCatServiceImpl implements TbItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<Object> findCatByParentId(Integer id) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        tbItemCatExample.createCriteria().andParentIdEqualTo(id.longValue());
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
        List<Object> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            CatTree catTree = new CatTree();
            catTree.setId(tbItemCat.getId());
            catTree.setText(tbItemCat.getName());
            if (tbItemCat.getIsParent()) {
                catTree.setState("closed");
            } else {
                catTree.setState("open");
            }
            list.add(catTree);
        }
        return list;
    }
}
